package com.one.controller;

import com.one.pojo.OneUser;
import com.one.pojo.Store;
import com.one.service.OneUserService;
import com.one.service.StoreService;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 库存管理
 * @Author semstouch
 * @Date 2017/4/8
 **/
@Controller
@ControllerAdvice
public class StroeController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private StoreService storeService;
    @Resource
    private OneUserService oneUserService;
    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;

    public StroeController(){
            super("callback");
    }
    @RequestMapping(value = "/queryMyStore", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult queryMyStore(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Store store  = storeService.findByUserId(userId);
        OneUser oneUser=oneUserService.findByUserId(userId);
        Map rs = new HashMap();
        rs.put("chicksNum",store.getChicksNum());
        rs.put("eggsNum",store.getEggsNum());
        rs.put("dogLevel",oneUser.getDogLevel());
        rs.put("fenceLevel",oneUser.getFenceLevel());
        rs.put("haveSuperIncubator",oneUser.getHaveSuperIncubator());
        rs.put("keyCleanDay",oneUser.getKeyCleanDay());
        jsonResult.setData(rs);
        jsonResult.setCode("1");
        return jsonResult;
    }

}
